package su.kozhevnikov.nookbrary.providers;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import su.kozhevnikov.nookbrary.Bibliotheca;
import su.kozhevnikov.nookbrary.Util;
import su.kozhevnikov.nookbrary.interfaces.*;
//import su.kozhevnikov.nookbrary.items.GoUpItem;
import su.kozhevnikov.nookbrary.items.ItemComparator;
import su.kozhevnikov.nookbrary.items.ItemFactory;

import android.util.Log;




public class FileItemProvider extends BaseItemProvider implements IItemProvider
{

	static final long TIMEOUT = 5 * 1000;//milliseconds
	
	String _path;
	IItemProvider _parent;
	int _itemsperpage;
	
	

	
	public FileItemProvider(String title, String path,IItemProvider parent)
	{
		_path = path;
		_parent = parent;
		_itemsperpage = Bibliotheca.ITEMSMAX;
		_title = title;

		
		
		
	}

	public String getInfo()
	{
		return _path==null ? Util.EMPTYSTR : _path;
	}
	
	protected void FillFromDir(File d, List<IItem> list) throws IOException {
		if (!d.isDirectory())
			return;
		Bibliotheca.showInfo("Scan " + d.getPath());
		//Log.d(Bibliotheca.LOGTAG, "Scan directory    : " + d.getPath());
		//Log.d(Bibliotheca.LOGTAG, "Scan directory (c): " + d.getCanonicalPath());
		IItem item;
		File[] subFiles = d.listFiles();
		for (int index = 0; index < subFiles.length; index++) {
			String fileName = subFiles[index].getName().toLowerCase();
			if (!subFiles[index].getPath().equalsIgnoreCase(subFiles[index].getCanonicalPath()))
				continue;
			if (subFiles[index].isDirectory()) {
				FillFromDir(subFiles[index], list);
			}
			else if (fileName.endsWith(".epub")
				|| fileName.endsWith(".fb2")
				|| fileName.endsWith(".fb2.zip")
			) {
				item = ItemFactory.FromFile(subFiles[index], this);
				if (item == null) continue;
				list.add(item);
			}
		}
	}
	
	protected void Fill()
	{

		_issorted = false;
		
		try
		{
			//long stop = System.currentTimeMillis()+TIMEOUT;

			
			File  f = new File(_path);
			if (!f.isDirectory()) 
				throw new Exception("Not folder");
			
			if (_title == null) _title = f.getName();

			List<IItem> list = new ArrayList<IItem>();

			this.FillFromDir(f, list);
			/*
			if (_parent!=null)
			{	_goupitem = new GoUpItem(_parent.getTitle(), _parent);
				//i.add(_goupitem);
			}
			File[] files = f.listFiles();
			int N = files.length;
			IItem item;
			for(int j=0;j<N;j++)
			{
				if (!files[j].isDirectory()) {
					item = ItemFactory.FromFile(files[j], this);
					if (item == null) continue;
					list.add(item);
				}
				else {
					File[] subFiles = files[j].listFiles();
					for (int index = 0; index < subFiles.length; index++) {
						String fileName = subFiles[index].getName().toLowerCase();
						if (fileName.endsWith(".epub")
							|| fileName.endsWith(".fb2")
							|| fileName.endsWith(".fb2.zip")
						) {
							item = ItemFactory.FromFile(subFiles[index], this);
							if (item == null) continue;
							list.add(item);
						}
					}
				}
			}
			*/
			
			Boolean istimeout = false;
			for(IItem i:list)
			{
				/*if (System.currentTimeMillis()>stop) 
				{
					istimeout = true;
					break;
				}*/
				i.Fill();
			}
			
			if (!istimeout)
			{
				Collections.sort(list, new ItemComparator.Default());
				_issorted = true;
			}
			
			_items = (IItem[])list.toArray(new IItem[list.size()]);
			


		}
		catch(Exception e)
		{
			_title = "Error '"+_path+"'";
			Log.d(Bibliotheca.LOGTAG,"Fill path '"+_path+"' error "+e.getMessage() );
			_items = null;
			_issorted = false;
		}
		
		
	}


	@Override
	protected void FillFiltered() {
	}
	
	




}
